var entityCollection = require('../../../Base/server/controllers/EntityCollection'),
    CBOEnum = require('../../../CBO/server/controllers/Enum.server.controller.js'),
    BaseFun = require('../../../Base/server/controllers/Function');

exports.findByUniqueID = function(uniqueID, completeFun){
	var entity = entityCollection.getEntity("Terminal");
	entity.findOne({UniqueID: uniqueID}, null, function(err, record){
		if(err){
			var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询终端错误';
            newErr.message = '请检查查询参数！';
            completeFun(newErr);
		}
		else{
			completeFun(null, record);
		}
	});
};

//创建一条终端记录
exports.insert = function(terminalData, completeFun) {
    var terminalEntity = entityCollection.getEntity("Terminal");
    terminalEntity.insert(terminalData, function(err, newRecord) {
        completeFun(err, newRecord);
    });
};

//更新一条终端记录
exports.updateByValue = function (uniqueID, value, completeFun) {
    var entity = entityCollection.getEntity("Terminal");
    entity.updateByValue({UniqueID: uniqueID}, value, function (err, result) {
        completeFun(err, result);
    });
};

//删除终端记录
// exports.deleteByUniqueID = function (uniqueID, value, completeFun) {
//     var entity = entityCollection.getEntity("Terminal");
//     entity.findOne({UniqueID: uniqueID}, null, function(err, record){
//         entity.delete(record, function (err, result) {
//             completeFun(err, result);
//         });
//     });
// };

//校验注册终端是否已经达到授权数
exports.registerCheck = function(){
    return new Promise(function(resolve, reject) {
        BaseFun.requestHttp({path: '/api/SystemManage/getLicense'}).then(function(license){
            var entity = entityCollection.getEntity("Terminal");
            entity.queryRecordCount({IsEffective: {$ne: false}}, function(err, count){
                if(err){
                    reject(err);
                }
                else{
                    if(count >= license.DeviceQty){
                        resolve(false);
                    }
                    else{
                        resolve(true);
                    }
                }
            });
        }).catch(
            err => reject(err)
        );
    });
};

//EMM创建终端服务
exports.createTerminalByEMM = function(req, responseFun){
    req.body.Terminal.SourceType = CBOEnum.TerminalSourceType.EMM.Value;
    exports.insert(req.body.Terminal, responseFun);
};

//EMM修改终端服务
exports.updateTerminalByEMM = function(req, responseFun){
    req.body.Terminal.SourceType = CBOEnum.TerminalSourceType.EMM.Value;
    exports.updateByValue(req.body.Terminal.UniqueID, req.body.Terminal, responseFun);
};

//EMM删除终端服务
exports.deleteTerminalByEMM = function(req, responseFun){
    var uniqueIDs = req.body.UniqueIDs;
    if(uniqueIDs && uniqueIDs.constructor == Array && uniqueIDs.length > 0){
        var entity = entityCollection.getEntity("Terminal");
        entity.deleteByCondition({UniqueID: {$in: req.body.UniqueIDs}}, responseFun);
    }
    else{
        responseFun(null, true);
    }
};

//移动端创建终端服务
exports.createTerminalByMobile = function(req, responseFun){
    req.body.Terminal.SourceType = CBOEnum.TerminalSourceType.Mobile.Value;
    exports.insert(req.body.Terminal, responseFun);
};
